<template>
  <div>
    <MBX
      level="商品管理"
      level2="商品分类"
    ></MBX>
    <br>
    <el-card>
      <el-row>
        <el-col>
          <el-button
            type="primary"
            @click="addcate"
          >添加分类</el-button>
        </el-col>
      </el-row>
      <!--表格 -->
      <el-table
        :data="cate"
        row-key="cat_id"
      >
        <el-table-column
          label="#"
          prop="index"
        ></el-table-column>
        <el-table-column
          label="分类名称"
          prop="cat_name"
        ></el-table-column>
        <el-table-column
          label="是否有效"
          prop="cat_deleted"
        >
          <template slot-scope="scope">
            <div>
              <i
                class="el-icon-success"
                v-if="scope.row.cat_deleted==false"
              ></i>
              <i
                v-else
                class="el-icon-error"
              ></i>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="排序"
          prop="cat_level"
        >
          <template slot-scope="scope">
            <div>
              <el-tag v-if="scope.row.cat_level==0">一级</el-tag>
              <el-tag
                v-else-if="scope.row.cat_level==1"
                type="success"
              >二级</el-tag>
              <el-tag
                v-else
                type="info"
              >三级</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div>
              <el-button
                type="primary"
                icon="el-icon-edit"
                @click="cateedit(scope.row)"
              ></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                @click="catedel(scope.row)"
              ></el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--表格 -->
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pagenum"
        :page-sizes="[2, 5, 10, 15]"
        :page-size="page.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 分页 -->
    </el-card>
    <!-- 编辑模态框 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form :model="cateeditform">
        <el-form-item prop="分类名称">
          <el-input v-model="cateeditform.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="editcateok"
        >确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑模态框 -->
    <!-- 添加模态框 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible1"
      width="30%"
    >
      <el-form :model="cateaddform">
        <el-form-item prop="分类名称">
          分类名称：{{cateaddform.cat_name}}
          <el-input v-model="cateaddform.cat_name"></el-input>
        </el-form-item>
        <el-form-item prop="父级分类">
          父级分类：{{value}}{{cateid}}
          <el-cascader
            v-model="value"
            :options="tianjialist"
            :props="propsProps"
            @change="handleChange"
            ref="addcateref"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addcateok"
        >确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加模态框 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      page: {
        pagenum: 1,
        pagesize: 2
      },
      cate: [],
      total: 0,
      dialogVisible: false,
      dialogVisible1: false,
      cateeditform: {
        cat_name: "",
        id: 0
      },
      cateaddform: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0
      },
      propsProps: {
        expandTrigger: "hover",
        children: "children",
        value: "cat_id",
        label: "cat_name",
        checkStrictly: true
      },
      value: [],
      tianjialist: []
    };
  },
  created() {
    this.xr();
  },
  methods: {
    async xr() {
      let res = await this.$API.shangpinfenlei(this.page);

      res.result.forEach((item, index) => {
        item.index = index + 1;
      });
      console.log("商品分类", res);
      this.cate = res.result;
      this.total = res.total;
    },
    handleSizeChange(v) {
      this.page.pagesize = vthis.xr();
    },
    handleCurrentChange(v) {
      this.page.pagenum = v;
      this.xr();
    },
    // 打开模态框回填
    cateedit(row) {
      console.log("编辑", row);
      this.dialogVisible = true;
      this.cateeditform.cat_name = row.cat_name;
      this.cateeditform.id = row.cat_id;
    },
    // 提交编辑
    async editcateok() {
      let res = this.$API.bianjitijiaofenlei(
        this.cateeditform.id,
        this.cateeditform.cat_name
      );
      this.xr()
      console.log("提交编辑", res);
      this.dialogVisible = false;
    },
    // 删除
    async catedel(row) {
      console.log("删除", row);
      let res = await this.$API.shanchufenlei(row.cat_id);
      this.xr();
    },
    // 添加分类弹出模态框
    async addcate() {
      this.dialogVisible1 = true;
      let res = await this.$API.shangpinfenlei({ type: "2" });
      console.log("添加模态框", res);
      this.tianjialist = res;
    },
    // 点击确定添加分类
    async addcateok() {
       console.log("111111",this.cateaddform);
      if (this.value.length > 0) {
        this.cateaddform.cat_pid = this.cateid;
        this.cateaddform.cat_level = this.value.length;
      } else {
        this.cateaddform.cat_pid = 0;
        this.cateaddform.cat_level = 0;
      }
     
      let res = await this.$API.tianjiafenlei(this.cateaddform);
      this.xr()
      this.dialogVisible1=false
      this.cateaddform={}
      this.value=[]
    },
    handleChange() {
      console.log("当前选择");
      this.$refs.addcateref.dropDownVisible = false;
    }
  },
  computed: {
    cateid() {
      return this.value[this.value.length - 1];
    }
  }
};
</script>

<style>
.el-radio__input {
  /* border: 1px solid; */
  position: absolute;
  top: -8px;
  left: 0;
  width: 130px;
  height: 20px;
}
.el-radio__inner {
  display: none;
}
</style>
